package cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.simplenotice;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.NoticeStateChange;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import io.realm.Realm;
import io.realm.Sort;

public class SampleNoticeActivity extends BaseActivity {


    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_msg_list)
    RecyclerView mRvMsgList;

    List<SampleNotice> mSampleNotices = new ArrayList<>();

    SampleNoticeAdapter mSampleNoticeAdapter;


    @Override
    public int initContentView() {
        return R.layout.activity_sample_notice;
    }

    @Override
    public void initInjector() {

    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        mTopBar.setTitle(getTitle().toString());
        setSupportActionBar(mTopBar.getToolbar());

        mSampleNoticeAdapter = new SampleNoticeAdapter(mSampleNotices);
        mRvMsgList.setLayoutManager(new LinearLayoutManager(mContext));
        mRvMsgList.setAdapter(mSampleNoticeAdapter);


        fetchSampleNotice();
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    private void fetchSampleNotice() {
        Realm.getDefaultInstance().executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                mSampleNotices.clear();

                for (SampleNotice notice : realm.where(SampleNotice.class).findAllSorted(SampleNotice.NOTICE_FIELD_TIME, Sort.DESCENDING)) {
                    notice.setReaded(true);

                    LogUtil.json(notice);
                    mSampleNotices.add(notice);
                }
                EventBus.getDefault().post(new NoticeStateChange());
                mSampleNoticeAdapter.notifyDataSetChanged();
            }
        });
    }






}
